package com.astronomy.base.controller;

import com.astronomy.base.pojo.Label;
import com.astronomy.base.service.LabelService;
import com.astronomy.entity.PageResult;
import com.astronomy.entity.Result;
import com.astronomy.entity.StatusCode;
import com.astronomy.states.Status;
import com.github.pagehelper.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/label")
@CrossOrigin
public class LabelController {


    @Autowired
    private LabelService labelService;

    /**
     * 查询全部标签
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Label> labelList = labelService.findAll();
        return new Result(true, StatusCode.OK, Status.SELECT_SUCCESS,labelList) ;
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        Label label = labelService.findById(id);
        return new Result(true,StatusCode.OK,Status.SELECT_SUCCESS,label);
    }

    @PostMapping
    public Result add(@RequestBody Label label){
        labelService.add(label);
        return new Result(true,StatusCode.OK,Status.ADD_SUCCESS);
    }

    @PutMapping(value="/{id}")
    public Result update(@RequestBody Label label, @PathVariable String id){
        label.setId(id);
        labelService.update(label);
        return new Result(true,StatusCode.OK,Status.UPDATE_SUCCESS);
    }

    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        labelService.delete(id);
        return new Result(true,StatusCode.OK,Status.DELETE_SUCCESS);
    }

    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@PathVariable  int page, @PathVariable  int size){
        Page<Label> pageList = labelService.findPage(page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,Status.SELECT_SUCCESS,pageResult);
    }

    @PostMapping(value = "/search" )
    public Result findList(@RequestBody Map searchMap){
        List<Label> list = labelService.findList(searchMap);
        return new Result(true,StatusCode.OK,Status.SELECT_SUCCESS,list);
    }

    @PostMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestBody Map searchMap, @PathVariable  int page, @PathVariable  int size){
        PageResult<Label> pageResult = labelService.findPage(searchMap, page, size);
        return new Result(true,StatusCode.OK,Status.SELECT_SUCCESS,pageResult);
    }

}
